package cn.demengsite.service.impl;

import cn.demengsite.dao.ProductDAO;
import cn.demengsite.model.BaseInfo;
import cn.demengsite.model.Product;
import cn.demengsite.model.ProductImageAttr;
import cn.demengsite.service.ProductService;
import cn.demengsite.utils.ProductUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDAO productDAO;


    @Override
    public BaseInfo getProductByBarCode(String barCode) {

        BaseInfo baseInfo ;

        //对传入的barCode进行判断，如果是自定义的则不进行查数据库操作
        if(barCode.length()==10){
            Product product = ProductUtils.getProductByBarCode(barCode);
            baseInfo= new BaseInfo("0","成功获取商品信息",product);

        }else{
            Product product  = productDAO.getProductByBarCode(barCode);
            if (product != null) {
                product.setSinglePrice(product.getOriginalPrice());
                baseInfo= new BaseInfo("0","成功获取商品信息",product);
            }else {
                baseInfo = new BaseInfo("100","没有找到对应商品");
            }
        }

        return  baseInfo;
    }

    @Override
    public BaseInfo getProductImageByProductId(String productId) {
        List<ProductImageAttr> imageAttrs = productDAO.getProductImageByProductId(productId);
        return new BaseInfo("0","OK",imageAttrs);
    }

    @Override
    public BaseInfo deleteProductImageById(String id) {
        productDAO.deleteProductImageById(id);
        return new BaseInfo("0","ok");
    }

    @Override
    public BaseInfo updateProductImage(ProductImageAttr imageAttr) {
        productDAO.updateProductImage(imageAttr);
        return new BaseInfo("0","ok");
    }
}
